package com.mrd.gtimp.sup.action;

import com.mrd.constant.MsgConstants;
import com.mrd.exception.QueryException;
import com.mrd.exception.SaveException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.base.entity.SystemValue;
import com.mrd.gtimp.base.service.SystemValueBiz;
import com.mrd.gtimp.sup.entity.SysBizAuto;
import com.mrd.gtimp.sup.service.SysBizAutoBiz;
import com.mrd.util.LoginUtil;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author hejj
 */

@Controller
@RequestMapping("/sup")
public class SysBizAutoAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private SysBizAutoBiz sysBizAutoBiz;

    @Resource
    private SystemValueBiz systemValueBiz;

    /**
     * public String show() { try { page = initPage(SysBizAutoBiz.findEntityCount(filters)); Order
     * order = new Order(); order.add(sidx, sord); SysBizAutos = SysBizAutoBiz
     * .findEntityByFilter(page.getCurrentPageStartNo(), page.getPageSize(), filters, order); }
     * catch (Exception e) { e.printStackTrace(); } return SUCCESS; }
     **/

    @Resource
    public void setSysBizAutoBiz(SysBizAutoBiz sysBizAutoBiz) {
        this.sysBizAutoBiz = sysBizAutoBiz;
    }

    @RequestMapping("/sysBizAuto-main")
    public String main() {
        return "/gtimp/sup/sysBizAuto-main";
    }


    @RequestMapping("/sysBizAuto-manage")
    @ResponseBody
    public String manage(SysBizAuto sysBizAuto, Jqgrid jqgrid) {
        try {
//            if (filters == null) {
//                filters = new Filter();
//                filters.setGroupOp(FilterGroupOption.AND);
//            }
            SearchRule searchRule = new SearchRule("identifier", SearchRuleOption.eq, sysBizAuto.getSystemValue().getIdentifier());
            jqgrid.addRule(searchRule);
//            systemValue = systemValueBiz.findEntityByFilter(0, 0, filters, null).get(0);
            Page<SystemValue> systemValue = systemValueBiz.findByJqgrid(jqgrid);
//            return searchRule;
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return "/gtimp/sup/sysBizAuto-manage";
    }

    @RequestMapping("/sysBizAuto-getData")
    @ResponseBody
    public Page<SysBizAuto> getData(SysBizAuto sysBizAuto, Jqgrid jqgrid) {
        try {
            LoginUtil.getLoginUser().getCompany().getId();
            SearchRule searchRule = new SearchRule("fromCompany.id", SearchRuleOption.eq, LoginUtil.getLoginUser()
                    .getCompany().getId());
            jqgrid.addRule(searchRule);
            searchRule = new SearchRule("systemValue.identifier", SearchRuleOption.eq, sysBizAuto
                    .getSystemValue().getIdentifier());
            jqgrid.addRule(searchRule);
            searchRule = new SearchRule("toCompany.id", SearchRuleOption.eq, sysBizAuto
                    .getToCompany()
                    .getId());
            jqgrid.addRule(searchRule);
//            page = initPage(sysBizAutoBiz.findEntityCount(filters));
//            sysBizAutos = sysBizAutoBiz.findEntityByFilter(page.getCurrentPageStartNo(), page
//                    .getPageSize(), filters, null);
            Page<SysBizAuto> sysBizAutos = sysBizAutoBiz.findByJqgrid(jqgrid);
            return sysBizAutos;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/sysBizAuto-saveOrUpdateSysBizAuto")
    @ResponseBody
    public Result saveOrUpdateSysBizAuto(SysBizAuto sysBizAuto) {
        Result msg;
        List<SysBizAuto> sysBizAutos = new ArrayList<>();
        try {
            this.getData(sysBizAuto, new Jqgrid());
            if (!sysBizAutos.isEmpty()) {
                sysBizAuto.setId(sysBizAutos.get(0).getId());
                sysBizAuto.setCreateuser(sysBizAutos.get(0).getCreateuser());
                sysBizAuto.setCreatedate(sysBizAutos.get(0).getCreatedate());
            } else {
                sysBizAuto.setId(null);
                sysBizAuto.setCreateuser(LoginUtil.getLoginUser().getAccount());
                sysBizAuto.setCreatedate(new Date());
            }
            sysBizAuto.setFromCompany(LoginUtil.getLoginUser().getCompany());
            sysBizAutoBiz.saveOrupdateEntity(sysBizAuto);
            msg = Result.newSuccessInstance(MsgConstants.OPERATION_SUCCESS);
        } catch (SaveException e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.OPERATION_FAIL);
        }
        return msg;
    }
}
